Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@sanity/eventsource
Advanced tools
@sanity/eventsource is a JavaScript library that provides a polyfill for the EventSource API, which is used for receiving server-sent events (SSE). This package allows you to establish a persistent connection to a server and receive updates in real-time, making it useful for applications that require live data updates, such as chat applications, live feeds, or notifications.
Establishing a Connection
This feature allows you to establish a connection to a server that sends events. The code sample demonstrates how to create a new EventSource instance and listen for incoming messages.
const EventSource = require('@sanity/eventsource');
const es = new EventSource('http://example.com/events');
es.onmessage = function(event) {
console.log('New message:', event.data);
};
Handling Different Event Types
This feature allows you to listen for specific types of events sent by the server. The code sample shows how to add an event listener for a custom event type.
const EventSource = require('@sanity/eventsource');
const es = new EventSource('http://example.com/events');
es.addEventListener('customEvent', function(event) {
console.log('Custom event received:', event.data);
});
Handling Connection Errors
This feature allows you to handle errors that occur during the connection. The code sample demonstrates how to set up an error handler for the EventSource instance.
const EventSource = require('@sanity/eventsource');
const es = new EventSource('http://example.com/events');
es.onerror = function(event) {
console.error('Error occurred:', event);
};
The 'eventsource' package is another polyfill for the EventSource API. It provides similar functionality to @sanity/eventsource, allowing you to receive server-sent events in environments that do not natively support EventSource. Both packages offer similar APIs and can be used interchangeably.
The 'sse.js' package is a lightweight library for handling server-sent events. It provides a simple API for establishing connections and listening for events. Compared to @sanity/eventsource, sse.js is more minimalistic and may lack some advanced features but is easier to use for basic use cases.
The 'reconnecting-eventsource' package extends the EventSource API with automatic reconnection capabilities. This package is useful if you need a more robust solution that can handle intermittent network issues. It offers additional features compared to @sanity/eventsource, such as configurable reconnection strategies.
Meta-package to make browsers and Node use different EventSource polyfills
npm install @sanity/eventsource
import polyfilledEventSource from '@sanity/eventsource'
The polyfill adds support for auth headers, which isn't part of the EventSource spec.
In NodeJS environments it's the implementation from eventsource package. While browsers use event-source-polyfill.
To get the browser implementation, no matter how your bundler or runtime understands package.json
exports
, you can use the following:
import polyfilledEventSource from '@sanity/eventsource/browser'
To force the Node implementation, use the following:
import polyfilledEventSource from '@sanity/eventsource/node'
MIT © Sanity.io
FAQs
EventSource polyfill for browser and node.js
We found that @sanity/eventsource demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 48 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.